package com.example.mdc.logback.module.service;

import com.example.mdc.logback.common.utils.OkHttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * @author liu_pc
 */
@Service("helloServiceName")
public class HelloService {

    private final Logger logger = LoggerFactory.getLogger(HelloService.class);

    private final HelloAsyncService helloAsyncService;

    /**
     * Instantiates a new Hello service.
     *
     * @param helloAsyncService the hello async service
     */
    public HelloService(HelloAsyncService helloAsyncService) {
        this.helloAsyncService = helloAsyncService;
    }


    /**
     * 测试MDC异步任务全局链路
     *
     * @param param 请求参数
     * @return new String Info
     */
    public String test(String param) {
        logger.info("测试MDC test 接口开始，请求参数:{}", param);
        helloAsyncService.helloAsync(param);

        try {
            String sync = OkHttpUtil.builder().addParam("test", "hello").url("http://localhost:9527/test/hello").get().sync();
            logger.info("GET-接口返回信息:{}", sync);

            String testJson = "{\"id\":123,\"name\":\"John Doe\",\"age\":28,\"email\":\"johndoe@example.com\",\"address\":{\"street\":\"123 Main St\",\"city\":\"New York\",\"state\":\"NY\",\"zipCode\":\"10001\"},\"phoneNumbers\":[\"+1-555-123-4567\",\"+1-555-987-6543\"]}";
            String postSync = OkHttpUtil.builder().url("http://localhost:8080/test/menu").post(testJson).sync();
            logger.info("POST-postSync:{}", postSync);
        } catch (Exception e) {
            logger.error("调用接口异常", e);
            throw new RuntimeException(e);
        }
        return "hello";
    }
}
